using System;
using System.Linq;
using UnityEngine;
using QFramework;
using Random = UnityEngine.Random;

namespace XCZProject
{
	/// <summary>
	/// 角色攻击属性类，控制角色攻击力、攻击频率等
	/// </summary>
	public partial class SimpleSword : ViewController
	{
		private float mCurrentSeconds = 0;

		//public BindableProperty<bool> SuperSimpleSword = new BindableProperty<bool>(true);

		private void Update()
		{
			mCurrentSeconds += Time.deltaTime;
			if (mCurrentSeconds>=Global.SimpleAbilityDuration.Value)
			{
				mCurrentSeconds = 0;
				
				// 如果是超级武器阶段，则数量、倍率、距离都会提升
				var countTimes = Global.SuperSword.Value ? 2 : 1;
				var damageTimes = Global.SuperSword.Value ? Random.Range(2,3+1) : 1;
				var distanceTimes = Global.SuperSword.Value ? 2 : 1;
				
				// 获取所有的敌人
				var enemies = FindObjectsByType<Enemy>(FindObjectsInactive.Exclude,FindObjectsSortMode.None);

				// 遍历所有敌人，如果和玩家距离过近，则闪一下红色
				// OrderBy为排序，根据玩家和敌人的距离进行从小到大排序
				foreach (var enemy in enemies
					         .OrderBy(e=>e.Direction2DFrom(Player.Default).magnitude)
					         .Where(e=>e.Direction2DFrom(Player.Default).magnitude<=Global.SimpleSwordRange.Value*distanceTimes)
					         .Take((Global.SimpleSwordCount.Value+Global.SimpleFlyCount.Value)*countTimes))// 先排序，再拿出前N个
				{
					Sword.Instantiate()
						.Position(enemy.Position() + Vector3.left * 0.25f)
						.Show()
						.Self(self =>
						{
							var selfCache = self;
							selfCache.OnTriggerEnter2DEvent(collider2D =>
							{
								var hurtBox = collider2D.GetComponent<HurtBox>();
								if (hurtBox)
								{
									if (hurtBox.Owner.CompareTag("Enemy"))
									{
										//enemy.Hurt(Global.SimpleAbilityDamage.Value);
										DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value*damageTimes,hurtBox.Owner.GetComponent<IEnemy>());
									}
								}
							}).UnRegisterWhenGameObjectDestroyed(gameObject);
							
							// 劈砍动画
							ActionKit
								.Sequence() // 开启一个序列任务
								.Callback(() => { selfCache.enabled = false;})// 先设置小刀的enable为false
								.Parallel(p =>
								{
									// 这是一个并行任务
									p.Lerp(0, 10, 0.2f, (z) =>
									{
										// self.transform.localEulerAngles =
										// 	// 下面是qframework的方法，根据selfcache的localAngels的Z轴，赋值z返还一个欧拉角，再赋值给小刀
										// 	selfCache.transform.localEulerAngles.Z(z);
										selfCache.LocalEulerAnglesZ(z);// 等于上面的内容
									});

									p.Append(
										ActionKit.Sequence()
											.Lerp(0,1.25f,0.1f,scale=>selfCache.LocalScale(scale))
											.Lerp(1.25f,1f,0.1f,scale=>selfCache.LocalScale(scale))
									);
								})
								.Callback(() =>
								{
									// 开始进行劈砍操作
									selfCache.enabled = true;
								})
								.Parallel(p =>
								{
									// 这是一个并行任务
									p.Lerp(10, -180, 0.2f, (z) =>
									{
										// self.transform.localEulerAngles =
										// 	// 下面是qframework的方法，根据selfcache的localAngels的Z轴，赋值z返还一个欧拉角，再赋值给小刀
										// 	selfCache.transform.localEulerAngles.Z(z);
										selfCache.LocalEulerAnglesZ(z);// 等于上面的内容
									});

									p.Append(
										ActionKit.Sequence()
											.Lerp(1,1.25f,0.1f,scale=>selfCache.LocalScale(scale))
											.Lerp(1.25f,1f,0.1f,scale=>selfCache.LocalScale(scale))
									);
								})
								.Callback(() => { selfCache.enabled = false;})
								.Lerp(-180,0,0.3f, z =>
								{
									// self.transform.localEulerAngles =
									// 	// 下面是qframework的方法，根据selfcache的localAngels的Z轴，赋值z返还一个欧拉角，再赋值给小刀
									// 	selfCache.transform.localEulerAngles.Z(z);
									selfCache.LocalEulerAnglesZ(z);// 等于上面的内容
									selfCache.LocalScale(z.Abs() / 180);
								})
								.Start(this, () =>
								{
									selfCache.DestroyGameObjGracefully();
								});
						});
					
				}
			}
		}
	}
}
